package tool

import play.api.data.Forms._
import play.api.data._
import tool.Pojo._
import play.api.data.JodaForms.jodaDate


/**
 * Created by yz on 2018/7/17
 */

class FormTool {

  case class AccountData(account: String, password: String)

  val accountForm = Form(
    mapping(
      "account" -> text,
      "password" -> text
    )(AccountData.apply)(AccountData.unapply)
  )

  val configNameForm = Form(
    mapping(
      "configName" -> text
    )(ConfigNameData.apply)(ConfigNameData.unapply)
  )

  val idOpForm = Form(
    mapping(
      "id" -> optional(number)
    )(IdOpData.apply)(IdOpData.unapply)
  )

  val saveConfigForm = Form(
    mapping(
      "missionName" -> text,
      "configName" -> text
    )(ConfigFormData.apply)(ConfigFormData.unapply)
  )
  val unitForm = Form(
    mapping(
      "unit" -> text
    )(UnitData.apply)(UnitData.unapply)
  )

  case class IdData(id: Int)

  val idForm = Form(
    mapping(
      "id" -> number
    )(IdData.apply)(IdData.unapply)
  )

  val sampleIdForm = Form(
    mapping(
      "id" -> text
    )(SampleIdData.apply)(SampleIdData.unapply)
  )

  case class MissionNameData(missionName: String)

  val missionNameForm = Form(
    mapping(
      "missionName" -> text
    )(MissionNameData.apply)(MissionNameData.unapply)
  )

  case class AdjustMissionData(mission: String)

  val adjustMissionForm = Form(
    mapping(
      "mission" -> text,
    )(AdjustMissionData.apply)(AdjustMissionData.unapply)
  )

  case class MissionIdOptionData(missionId: Option[Int])

  val missionIdOptionForm = Form(
    mapping(
      "missionId" -> optional(number)
    )(MissionIdOptionData.apply)(MissionIdOptionData.unapply)
  )

  case class ArgsData(missionId: Int, compoundName: String)

  val argsForm = Form(
    mapping(
      "missionId" -> number,
      "compoundName" -> text,
    )(ArgsData.apply)(ArgsData.unapply)
  )

  val strForm = Form(
    mapping(
      "strSiteNames" -> seq(text),
      "strSiteDatas" -> seq(text)
    )(StrData.apply)(StrData.unapply)
  )

  val snpForm = Form(
    mapping(
      "snpSiteNames" -> seq(text),
      "snpSiteDatas" -> seq(text)
    )(SnpData.apply)(SnpData.unapply)
  )

  case class ParamAdjustData(missionName: String, missionId: Int, compoundName: String, flMin: String, flMax: String, step: String, nups: String,
                             ndowns: String, snr: String, iteration: String, bLine: String, rtlw: String, rtrw: String,
                             rt: String, peakLocation: String)

  val paramAdjustForm = Form(
    mapping(
      "missionName" -> text,
      "missionId" -> number,
      "compoundName" -> text,
      "flMin" -> text,
      "flMax" -> text,
      "step" -> text,
      "nups" -> text,
      "ndowns" -> text,
      "snr" -> text,
      "iteration" -> text,
      "bLine" -> text,
      "rtlw" -> text,
      "rtrw" -> text,
      "rt" -> text,
      "peakLocation" -> text
    )(ParamAdjustData.apply)(ParamAdjustData.unapply)
  )

  case class UnitConversionData(unit: String, toUnit: String, fromC: Int)

  val unitConversionForm = Form(
    mapping(
      "unit" -> text,
      "toUnit" -> text,
      "fromC" -> number
    )(UnitConversionData.apply)(UnitConversionData.unapply)
  )

  case class FileNameData(fileName: String)

  val fileNameForm = Form(
    mapping(
      "fileName" -> text
    )(FileNameData.apply)(FileNameData.unapply)
  )

  case class FileNamesData(fileNames: Seq[String])

  val fileNamesForm = Form(
    mapping(
      "fileNames" -> seq(text)
    )(FileNamesData.apply)(FileNamesData.unapply)
  )

}

object FormTool {

  val userNameForm = Form(
    mapping(
      "name" -> text
    )(UserNameData.apply)(UserNameData.unapply)
  )

  val missionNameCheckForm = Form(
    mapping(
      "missionName" -> text,
      "kind" -> text,
    )(MissionNameCheckData.apply)(MissionNameCheckData.unapply)
  )

  val searchForm = Form(
    mapping(
      "missionName" -> optional(text),
      "missionIntro" -> optional(text),
      "startDate" -> optional(jodaDate),
      "endDate" -> optional(jodaDate)
    )(SearchData.apply)(SearchData.unapply)
  )

  val sampleSearchForm = Form(
    mapping(
      "id" -> text,
      "indexId" -> text,
      "sampleIntro" -> text,
      "gender" -> text,
      "kind" -> text,
      "comment" -> text,
      "updateMission" -> text,
      "startDate" -> optional(jodaDate),
      "endDate" -> optional(jodaDate)
    )(SampleSearchData.apply)(SampleSearchData.unapply)
  )

  val missionForm = Form(
    mapping(
      "missionName" -> text,
      "lane" -> text,
      "id" -> text,
      "gender" -> text,
      "kind" -> text
    )(MissionData.apply)(MissionData.unapply)
  )

  val long2fatForm = Form(
    mapping(
      "missionName" -> text,
      "inputKind" -> text,
      "fileContent" -> text
    )(Long2fatData.apply)(Long2fatData.unapply)
  )

  val fileNameForm = Form(
    mapping(
      "fileName" -> text
    )(FileNameData.apply)(FileNameData.unapply)
  )

  val sampleIdForm = Form(
    mapping(
      "id" -> text
    )(SampleIdData.apply)(SampleIdData.unapply)
  )

  val sampleSummaryForm = Form(
    mapping(
      "id" -> text,
      "panel" -> text
    )(SampleSummaryData.apply)(SampleSummaryData.unapply)
  )

  val kindForm = Form(
    mapping(
      "kind" -> text
    )(KindData.apply)(KindData.unapply)
  )

  val missionNameForm = Form(
    mapping(
      "missionName" -> text
    )(MissionNameData.apply)(MissionNameData.unapply)
  )

  val idForm = Form(
    mapping(
      "id" -> number
    )(IdData.apply)(IdData.unapply)
  )

  val barcodeForm = Form(
    mapping(
      "barcode" -> text
    )(BarcodeData.apply)(BarcodeData.unapply)
  )

  val sampleNameForm = Form(
    mapping(
      "sampleName" -> text
    )(SampleNameData.apply)(SampleNameData.unapply)
  )

  val refGeneForm = Form(
    mapping(
      "refGene" -> text
    )(RefGeneData.apply)(RefGeneData.unapply)
  )

  val idsForm = Form(
    mapping(
      "ids" -> seq(number)
    )(IdsData.apply)(IdsData.unapply)
  )

  val exampleForm = Form(
    mapping(
      "kind" -> text,
      "fileName" -> text
    )(ExampleData.apply)(ExampleData.unapply)
  )


  val stepForm = Form(
    mapping(
      "missionName" -> text,
      "stepNum" -> number,
      "id" -> text,
      "kitName" -> text
    )(StepData.apply)(StepData.unapply)
  )

  val batchMissionForm = Form(
    mapping(
      "missionIntro" -> text,
      "dataType" -> text,
      "splitDir" -> text,
      "serverGzFile" -> text,
      "isCover" -> optional(boolean)
    )(BatchMissionData.apply)(BatchMissionData.unapply)
  )

  val serverDirForm = Form(
    mapping(
      "splitDir" -> text
    )(ServerDirData.apply)(ServerDirData.unapply)
  )

  val adminSetForm = Form(
    mapping(
      "gzPath" -> text,
    )(AdminSetData.apply)(AdminSetData.unapply)
  )

  val nodeForm = Form(
    mapping(
      "id" -> text,
    )(NodeData.apply)(NodeData.unapply)
  )

  val missionIdForm = Form(
    mapping(
      "missionId" -> number
    )(MissionIdData.apply)(MissionIdData.unapply)
  )

  val siteNamesForm = Form(
    mapping(
      "siteNames" -> seq(text)
    )(SiteNamesData.apply)(SiteNamesData.unapply)
  )

  val columnNamesForm = Form(
    mapping(
      "columnNames" -> seq(text)
    )(ColumnNamesData.apply)(ColumnNamesData.unapply)
  )

  val columnNameForm = Form(
    mapping(
      "columnName" -> text
    )(ColumnNameData.apply)(ColumnNameData.unapply)
  )

  val sexForm = Form(
    mapping(
      "sex" -> text
    )(SexData.apply)(SexData.unapply)
  )

  val compareMissionForm = Form(
    mapping(
      "userId" -> number,
      "missionName" -> text,
      "sample1" -> text,
      "sample2" -> text
    )(CompareMissionData.apply)(CompareMissionData.unapply)
  )

  val kitNameForm = Form(
    mapping(
      "kitName" -> text
    )(KitNameData.apply)(KitNameData.unapply)
  )

  val sampleIdsForm = Form(
    mapping(
      "ids" -> seq(text)
    )(SampleIdsData.apply)(SampleIdsData.unapply)
  )

  val pageForm = Form(
    mapping(
      "limit" -> number,
      "offset" -> number,
      "order" -> text,
      "search" -> optional(text),
      "sort" -> optional(text),
      "gender" -> optional(list(text)),
      "kind" -> optional(list(text)),
      "panel" -> optional(list(text)),
      "id" -> optional(text),
      "indexId" -> optional(text),
      "sampleIntro" -> optional(text),
      "comment" -> optional(text),
      "updateMission" -> optional(list(text)),
      "startTime" -> optional(jodaDate),
      "endTime" -> optional(jodaDate),
      "containerName" -> optional(list(text)),
      "singleSource" -> optional(list(text)),
      "autosomalLociTyped" -> optional(list(text)),
      "yLociTyped" -> optional(list(text)),
      "xLociTyped" -> optional(list(text)),
      "isnpLociTyped" -> optional(list(text)),
      "interlocusBalance" -> optional(list(text))
    )(PageData.apply)(PageData.unapply)
  )

  val phoneForm = Form(
    mapping(
      "phone" -> text
    )(PhoneData.apply)(PhoneData.unapply)
  )

  val newPhoneForm = Form(
    mapping(
      "newPhone" -> text
    )(NewPhoneData.apply)(NewPhoneData.unapply)
  )

  val emailForm = Form(
    mapping(
      "email" -> text
    )(EmailData.apply)(EmailData.unapply)
  )

  val registerUserForm = Form(
    mapping(
      "fullName" -> text,
      "password" -> text,
      "email" -> text,
      "phone" -> text,
      "company" -> text,
      "province" -> text,
      "city" -> text,
      "teacherName" -> text
    )(RegisterUserData.apply)(RegisterUserData.unapply)
  )

  val updateUserForm = Form(
    mapping(
      "fullName" -> text,
      "email" -> text,
      "phone" -> text,
      "institution" -> text,
      "province" -> text,
      "city" -> text,
      "teacher" -> text
    )(UpdateUserData.apply)(UpdateUserData.unapply)
  )

  val validCodeForm = Form(
    mapping(
      "phone" -> text,
      "validCode" -> text
    )(ValidCodeData.apply)(ValidCodeData.unapply)
  )

  val validEmailCodeForm = Form(
    mapping(
      "email" -> text,
      "validCode" -> text
    )(ValidEmailCodeData.apply)(ValidEmailCodeData.unapply)
  )

  val loginUserForm = Form(
    mapping(
      "email" -> text,
      "password" -> text
    )(LoginUserData.apply)(LoginUserData.unapply)
  )

  val newPasswordForm = Form(
    mapping(
      "phone" -> text,
      "password" -> text
    )(NewPasswordData.apply)(NewPasswordData.unapply)
  )

  val userForm = Form(
    mapping(
      "name" -> text,
      "password" -> text
    )(UserData.apply)(UserData.unapply)
  )

  val changePasswordForm = Form(
    mapping(
      "password" -> text,
      "newPassword" -> text
    )(ChangePasswordData.apply)(ChangePasswordData.unapply)
  )

}
